package com.aididi.designpatter;

/**
 * @Auther: xuzexiang
 * @Date: 2018-10-29 0029 11:24
 * @Description:    装饰器模式   给一类对象增加新的功能，装饰方法与具体的内部逻辑无关
 * 装饰器模式（Decorator Pattern）允许向一个现有的对象添加新的功能，同时又不改变其结构。
 * 这种类型的设计模式属于结构型模式，它是作为现有的类的一个包装。
 * 使用场景： 1、扩展一个类的功能。 2、动态增加功能，动态撤销。
 */
public class DecoratorDemo {
    public static void main(String[] args) {
    //使用 DecoratorDemo 来装饰 Shape 对象
        Shapes circle = new Circles();

        Shapes redCircle = new RedShapeDecorator(new Circles());

        Shapes redRectangle = new RedShapeDecorator(new Rectangles());
        System.out.println("Circle with normal border");
        circle.draw();

        System.out.println("\nCircle of red border");
        redCircle.draw();

        System.out.println("\nRectangle of red border");
        redRectangle.draw();
    }
}

/**
 * 创建一个接口：
 */
interface Shapes {
    void draw();
}
/**
 *创建实现接口的实体类
 */
class Rectangles implements Shapes{

    @Override
    public void draw() {
        System.out.println("Shape: Rectangle");
    }
}

class Circles implements Shapes{

    @Override
    public void draw() {
        System.out.println("Shape: Circle");
    }
}
/**
 *创建实现了 Shape 接口的抽象装饰类
 */
abstract class ShapeDecorator implements Shapes {
    protected Shapes decoratedShape;

    public ShapeDecorator(Shapes decoratedShape){
        this.decoratedShape = decoratedShape;
    }

    @Override
    public void draw(){
        decoratedShape.draw();
    }
}

/**
 *  创建扩展了 ShapeDecorator 类的实体装饰类
 */
class RedShapeDecorator extends ShapeDecorator {

    public RedShapeDecorator(Shapes decoratedShape) {
        super(decoratedShape);
    }

    @Override
    public void draw() {
        decoratedShape.draw();
        setRedBorder(decoratedShape);
    }

    private void setRedBorder(Shapes decoratedShape){
        System.out.println("Border Color: Red");
    }
}